<template>
	<div class="createXieyi w944">
		<alert ref="alert"></alert>
		<div class="label_top">
			<p><i class="line"></i>{{updateTitle}}</p>
		</div>
		<div class="createLine">
			<em>协议标题:</em>
			<input v-model="title" placeholder="请输入协议标题"/>
		</div>
		<div class="createLine">
			<em>协议内容:</em>
			<vue-ueditor-wrap v-model="agreementDesc" :config="myConfig" class="editor"></vue-ueditor-wrap>
		</div>
		<div class="footerButton">
			<a @click="checkSave" v-bind:style="{background:clickLock==true ? '#44CEF6' : '#3a8ee6'}">保存</a>
			<a @click="cancel" class="cancel">取消</a>
		</div>
	</div>
</template>

<script>
	import alert from '../../components/Alert.vue';
	export default{
		data(){
			return{
				clickLock:true,
				updateTitle:'',
				nowType: 'add',
				id:'',
				title:'',
				agreementDesc:'',
				myConfig: {
					autoHeightEnabled: false,
					initialFrameHeight: 420,
					initialFrameWidth: '840',
				}
			}
		},
		methods:{
			getParams() {
				var routerParams = this.$route.params.type;
				if(routerParams == 'edit') {
					this.nowType = 'edit';
					this.updateTitle = "编辑协议";
				}else{
					this.nowType = 'add';
					this.updateTitle = "新增协议";
				};
			},
			create(){
				if(this.clickLock==true){
					this.clickLock = false;
					this.$ajax({
						url: this.$root.jifuUrl + '/mns/userAgreement/addUserAgreement',
						headers:{'ContentType': 'application/json; charset=utf-8'},
						data: {
							title:this.title,
							agreementDesc:this.agreementDesc
						},
						method: 'POST',
						success: res => {
							if(res.data.code == 200) {
								this.$refs.alert.show({alertText: '保存成功'});
								setTimeout(() =>{
									this.$router.push({
										name:'xieyi'
									});
								},2000)
							}else{
								this.clickLock = true;
								this.failText = res.data.message;
								this.$rootFunction().deleteResFail();
							}
						}
					});
				}
			},
			update(){
				this.$ajax({
					url: this.$root.jifuUrl + '/mns/userAgreement/updateUserAgreement',
					headers:{'ContentType': 'application/json; charset=utf-8'},
					data: {
						id:this.id,
						title:this.title,
						agreementDesc:this.agreementDesc
					},
					method: 'POST',
					success: res => {
						if(res.data.code == 200) {
							this.$refs.alert.show({alertText: '保存成功'});
							setTimeout(() =>{
								this.$router.push({
									name:'xieyi'
								});
							},2000)
						}else{
							this.failText = res.data.message;
							this.$rootFunction().deleteResFail();
						}
					}
				});
			},
			checkSave() {
				if(this.nowType == 'add') {
					this.create();
				}else if(this.nowType == 'edit') {
					this.update();
				}
			},
			cancel() {
				this.$router.go(-1);
			}
		},
		components:{
			alert
		},
		mounted(){
			this.getParams();
			if(this.nowType == "edit") {
				console.log('306', this.nowType, this.$root.xieyiMsg);
				this.id = this.$root.xieyiMsg.id;
				this.title = this.$root.xieyiMsg.title;
				this.agreementDesc = this.$root.xieyiMsg.agreementDesc;
			}
		}
	}
</script>